package com.horizon.common.util;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.UUID;

/**
 * ID生成器
 */
public class IDGenerator {
    /**
     * 生成32位UUID
     *
     * @return
     */
    public static String generateID() {
        String rtnVal = Long.toHexString(System.currentTimeMillis());
        rtnVal += UUID.randomUUID();
        rtnVal = rtnVal.replaceAll("-", "");
        return rtnVal.substring(0, 32);
    }

    /**
     * 解析ID生成时间
     *
     * @param id
     * @return
     */
    public static LocalDateTime parseTime(String id) {
        String timeInfo = id.substring(0, 11);
        Instant instant = Instant.ofEpochMilli(Long.parseLong(timeInfo, 16));
        LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
        return localDateTime;
    }
}
